local moyin = fk.CreateSkill {
  name = "th_moyin",
  tags = { Skill.Compulsory },
}

-- 获取角色对应先辅Mark的实际值
---@param player Player @ 要被获取标记的那个玩家
---@param name string @ 标记名称（不带前缀）
---@return any
local getXianfuMark = function(player, name)
  return type(player:getMark("@[th_xianfu]" .. name)) == "table" and player:getMark("@[th_xianfu]" .. name).value or 0
end

-- 设置角色对应私人Mark
---@param player ServerPlayer @ 要被获取标记的那个玩家
---@param name string @ 标记名称（不带前缀）
---@param value any @ 标记实际值
---@param visible? boolean? @ 是否公开标记
local setXianfuMark = function(player, name, value, visible)
  local mark = player:getMark("@[th_xianfu]" .. name)
  if value == nil or value == 0 then
    player.room:setPlayerMark(player, "@[th_xianfu]" .. name, 0)
  else
    if type(mark) ~= "table" then mark = {} end
    mark.visible = visible
    mark.value = value
    player.room:setPlayerMark(player, "@[th_xianfu]" .. name, mark)
  end
end

-- 让一名角色的先辅mark对所有角色都可见
---@param player ServerPlayer @ 要公开标记的角色
---@param name? string @ 要公开的标记名（不含前缀）
local showXianfuMark = function(player, name)
  local room = player.room
  local mark = player:getMark("@[th_xianfu]" .. name)
  if type(mark) == "table" then
    mark.visible = true
    room:setPlayerMark(player, "@[th_xianfu]" .. name, mark)
  end
end

moyin:addLoseEffect(function(self, player, is_death)
  setXianfuMark(player, moyin.name, nil)
end)

moyin:addEffect(fk.GameStart, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(moyin.name)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local tos = room:askToChoosePlayers(player, {
      targets = room:getOtherPlayers(player, false),
      min_num = 1,
      max_num = 1,
      prompt = "#th_moyin-choose",
      skill_name = moyin.name,
      cancelable = false,
      no_indicate = true
    })
    setXianfuMark(player, moyin.name, tos[1].id)
  end,
})
moyin:addEffect(fk.DrawNCards, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    local mark = getXianfuMark(player, moyin.name)
    local moyin_target = player.room:getPlayerById(mark)
    if moyin_target and not moyin_target.dead then
      return target == player or target == moyin_target
    end
  end,
  on_use = function(self, event, target, player, data)
    data.n = data.n + 1
  end,
})
moyin:addEffect(fk.Death, {
  can_refresh = function(self, event, target, player, data)
    local mark = getXianfuMark(player, moyin.name)
    return mark ~= 0 and (target == player or mark == target.id)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local moyin_target = room:getPlayerById(getXianfuMark(player, moyin.name))
    local other = (target ~= player) and player or moyin_target
    setXianfuMark(player, moyin.name, 0)
    if not other.dead then
      room:setPlayerMark(other, "@@th_moyin", 1)
    end
  end
})

moyin:addEffect(fk.DrawNCards, {
  anim_type = "drawcard",
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:getMark("@@th_moyin") ~= 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.n = data.n + 2
  end
})
moyin:addEffect(fk.AfterDrawNCards, {
  is_delay_effect = true,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    local mark = getXianfuMark(player, "th_moyin")
    local moyin_target = player.room:getPlayerById(mark)
    if moyin_target and not moyin_target.dead then
      return (target == player or target == moyin_target) and not target:isKongcheng()
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local moyin_target = room:getPlayerById(getXianfuMark(player, "th_moyin"))
    local other = (target ~= player) and player or moyin_target
    showXianfuMark(player, "th_moyin")
    local cards = room:askToCards(target, {
      min_num = 1,
      max_num = #target:getCardIds("h"),
      include_equip = false,
      skill_name = moyin.name,
      cancelable = false,
      prompt = "#th_moyin-give::" .. other.id
    })
    room:obtainCard(other, cards, false, fk.ReasonGive)
    local n = #cards
    cards = room:askToCards(other, {
      min_num = n,
      max_num = n,
      include_equip = false,
      skill_name = moyin.name,
      cancelable = false,
      prompt = "#th_moyin-giveback::" .. target.id .. ":" .. n
    })
    room:obtainCard(target, cards, false, fk.ReasonGive)
  end,
})

--先辅mark
--作为table只有两个参数：
--具体值（value，若为数字，默认为武将id），和是否对其他人可见（visible）
Fk:addQmlMark{
  name = "th_xianfu",
  qml_path = "",
  how_to_show = function(name, value, p)
    if type(value) ~= "table" then return " " end
    if type(value) == "string" then value = tonumber(value) end --- 实际访问的时候这里会出string
    if (value.visible or Self == p) then
      if type(value.value) == "number" then
        local visual = Fk:currentRoom():getPlayerById(value.value)
        if visual then
          local ret = Fk:translate(visual.general)
          -- if visual.deputyGeneral and visual.deputyGeneral ~= "" then
          --   ret = ret .. "/" .. Fk:translate(visual.deputyGeneral)
          -- end
          return ret
        end
      end
      return tostring(value.value)
    end
    return "#hidden"
  end,
}

Fk:loadTranslationTable{
  ["th_moyin"] = "魔引",
  [":th_moyin"] = "锁定技，游戏开始时，你秘密选择一名其他角色，你与其摸牌阶段多摸一张牌并交给对方至少一张手牌，然后对方交回等量张手牌；一方死亡后，另一方摸牌阶段多摸两张牌。",

  ["#th_moyin-choose"] = "魔引: 请选择要与“魔引”联动的角色",
  ["#th_moyin-give"] = "魔引：你须交给 %dest 至少一张牌",
  ["#th_moyin-giveback"] = "魔引：你须交还 %dest %arg张牌",

  ["@[th_xianfu]th_moyin"] = "魔引",
  ["@@th_moyin"] = "魔引",
}

return moyin